package edu.info.manager.dao;

import edu.info.manager.domain.Student;

public class StudentDao {

    private static Student[] stus = new Student[5];

    public boolean add(Student stu) {

        //创建数组

        //遍历数组，查找null位置
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stud = stus[i];
            if (stud == null) {
                index = i;
                break;
            }
        }

        //将数据装入数组
        if (index == -1) {
            return false;
        } else {
            stus[index] = stu;
            return true;
        }
    }

    public Student[] findAllStudent() {

        return stus;
    }

    public void deleteStudentById(String id) {
        //查找id在数组中的索引位置
        int index = getIndex(id);
        //根据该索引位置，使用null元素进行覆盖
        stus[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null && stu.getId().equals(id)) {
                index = i;
                break;
            }
        }

        return index;
    }

    public void updateStudent(String uid, Student stu) {
        int index = getIndex(uid);
        stus[index] = stu;
    }
}
